package com.tony.demo.orientation;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.tony.demo.R;
import com.tony.demo.activity.BaseActivity;

public class OrientationActivity extends BaseActivity implements OnCheckedChangeListener {
	private CheckBox mFixOrientation;
	private Configuration mConfiguration;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_orientation_layout);

		mFixOrientation = (CheckBox) findViewById(R.id.cb_fix_orientation);
		mFixOrientation.setOnCheckedChangeListener(this);

		mConfiguration = getResources().getConfiguration();
//		if (mConfiguration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//			setRequestedOrientation(Configuration.ORIENTATION_PORTRAIT);
//			mConfiguration = getResources().getConfiguration();
//		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (mFixOrientation.isChecked()) {
			if(mConfiguration.orientation == Configuration.ORIENTATION_LANDSCAPE){
				setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
			}
		}
		else {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		}
		mConfiguration = getResources().getConfiguration();
	}

//	@Override
//	public void setRequestedOrientation(int requestedOrientation) {
//		// TODO Auto-generated method stub
////		if(mFixOrientation.isChecked()){
////			requestedOrientation = Configuration.ORIENTATION_PORTRAIT;
////		}
//		super.setRequestedOrientation(requestedOrientation);
//	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		if(mFixOrientation.isChecked()){
			newConfig = mConfiguration;
		}
		super.onConfigurationChanged(newConfig);
	}

}